﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.BikeManage;
using Ninject;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditLockArea : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();

        public EditLockArea()
        {
            InitializeComponent();
        }

        public EditLockArea(int areaid)
        {
            InitializeComponent();
            if (areaid > 0)
            {
                lbAreaId.Text = areaid.ToString(CultureInfo.InvariantCulture);
                ResultInfo<IList<jsh_Area>> area = _lockArea.GetLockArea(areaid);
                txtAreaName.Text = area.Obj[0].Area;
            }
        }

        private void btnSaveArea_Click(object sender, EventArgs e)
        {
            int id = Converts.ConvertToInt(lbAreaId.Text);
            var area = new jsh_Area {id = id, Area = txtAreaName.Text};
            ResultInfo<int> result = _lockArea.CreateOrUpdateLockArea(area);
            if (result.ResultNumber == ResultEnum.Success)
            {
                if (!string.IsNullOrEmpty(lbAreaId.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("修改停车区域信息成功！");
                }
                if (string.IsNullOrEmpty(lbAreaId.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("新增停车区域信息成功！");
                }
                DialogResult = DialogResult.OK;
            }
            else
            {
                MyMessageBox.ShowMessageBoxInfo(result.Describe);
            }
        }

        /// <summary>
        ///     取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}